package com.example.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.project.entity.Booking;
import com.example.project.entity.BookingVo;
import com.example.project.entity.Hotel;
import com.example.project.entity.User;
import com.example.project.mapper.BookingMapper;
import com.example.project.mapper.HotelMapper;
import com.example.project.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 预约信息Controller
 *
 * @author changyao
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/project/booking")
public class BookingController
{
    @Autowired
    private BookingMapper bookingMapper;

    @Autowired
    private UserMapper userMapper;


    @Autowired
    private HotelMapper hotelMapper;

    /**
     * 新增预约
     */
    @PostMapping("/insert")
    public int insert(@RequestBody Booking booking){
        // 看当前用户是否预约
        QueryWrapper<Booking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",booking.getUserId()).eq("goods_id",booking.getGoodsId());
        Booking booking1 = bookingMapper.selectOne(queryWrapper);
        booking.setCreateTime(LocalDateTime.now());
        if(booking1 != null){
            return 0;
        }else{
            bookingMapper.insert(booking);
            return 1;
        }

    }

    // 查询预约信息
    @GetMapping("/list")
    public List<BookingVo> getList(){
        List<BookingVo> bookingVos = new ArrayList<>();
        QueryWrapper<Booking> queryWrapper = new QueryWrapper<>();
        List<Booking> list = bookingMapper.selectList(queryWrapper);
        for (Booking booking : list) {
            BookingVo vo = new BookingVo();
            BeanUtils.copyProperties(booking,vo);
            User user = userMapper.selectById(booking.getUserId());
            Hotel hotel = hotelMapper.selectById(booking.getGoodsId());
            vo.setUserName(user.getUserNickname());
            vo.setGoodsName(hotel.getName());
            vo.setGoodsImg(hotel.getPhoto());
            vo.setDetails(hotel.getContent());
            vo.setPrice(hotel.getPrice().toString());
            bookingVos.add(vo);
        }

        return bookingVos;
    }


    // 取消预约
    @DeleteMapping("/{id}")
    public int delById(@PathVariable Long id){
        bookingMapper.deleteById(id);

        return 1;
    }



    /**
     * 获取预约信息详细信息
     */
    @PostMapping("/detil")
    public Booking getInfo(@RequestBody Booking booking)
    {
        return bookingMapper.selectById(booking);
    }


    /**
     * 新增预约信息
     */
    @PostMapping("/add")
    public int add(@RequestBody Booking booking)
    {
        return bookingMapper.insert(booking);
    }

    /**
     * 修改预约信息
     */
    @PostMapping("/edit")
    public int edit(@RequestBody Booking booking)
    {
        return bookingMapper.updateById(booking);
    }

    /**
     * 删除预约信息
     */
	@PostMapping("/del")
    public int remove(@RequestBody Booking booking)
    {
        return bookingMapper.deleteById(booking);
    }
}
